package com.yanq.liancar.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.SmallBModifyServiceActivity;
import com.yanq.liancar.activity.SmallBSeeServiceActivity;
import com.yanq.liancar.adapter.BSreviceAdmAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.FullServiceBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BSreviceAdmFragment extends BaseLYFragment {

    @BindView(R.id.recyclerview_service)
    RecyclerView mRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout mSmartRefreshLayout;
    private int type;
    private int page=1;
    private boolean isClear=true;
    private BSreviceAdmAdapter adapter;
    private List<FullServiceBean.DataBean> dataBeanList=new ArrayList<>();

    public static BSreviceAdmFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        //bundle.putString("where",where);
        BSreviceAdmFragment pageFragment = new BSreviceAdmFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_b_sreviceadm);
        type = getArguments().getInt("type");
        initListener();
        initRecyclerview();

    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        if (type==1){
            initOkGoService1();
        }else {
            initOkGoService();
        }
    }

    private void initRecyclerview() {
        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new BSreviceAdmAdapter(R.layout.item_small_b_sreviceadm, dataBeanList);
        mRecyclerview.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId()==R.id.item_tv_delete){          //删除
                    myDialog(position);
                }else if (view.getId()==R.id.item_tv_modify){    //修改
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("BSreviceAdmBean",dataBeanList.get(position));
                    RxActivityTool.skipActivity(mActivity, SmallBModifyServiceActivity.class,bundle);
                }else {                                           //查看
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("BSreviceAdmBean",dataBeanList.get(position));
                    RxActivityTool.skipActivity(mActivity, SmallBSeeServiceActivity.class,bundle);
                }
            }
        });
    }

    private void initListener() {
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;
                if (type!=1){
                    initOkGoService();
                }
                refreshlayout.finishRefresh();
            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (type!=1){
                    initOkGoService();
                }
                refreshLayout.finishLoadMore();
            }

        });
    }

    private void myDialog(final int pos){
        final Dialog bottomDialog = new Dialog(getContext());
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_commom, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = (int) (getResources().getDisplayMetrics().widthPixels *0.9);
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation2);
        bottomDialog.setCanceledOnTouchOutside(false);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();

        TextView tv_cancle = bottomDialog.findViewById(R.id.cancel);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bottomDialog.dismiss();
            }
        });
        TextView tv_submit = bottomDialog.findViewById(R.id.submit);
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataBeanList.get(pos).getServiceType()==80||dataBeanList.get(pos).getServiceType()==90){
                    initDeleteService1();
                }else {
                    initDeleteService(pos);
                }
                bottomDialog.dismiss();
            }
        });
    }

    //查询服务列表
    private void initOkGoService() {
        OkGo.<String>get(Config.s + "llc/showCarService")
                .tag(this)
                .params("token", UserBean.getbToken(mActivity))
                .params("type",type )
                .params("page",page )
                .params("num",4 )
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FullServiceBean bean = gson.fromJson(response.body(), FullServiceBean.class);
                        if (bean.isIsSuccess()) {
                            if (isClear){
                                dataBeanList.clear();
                            }
                            dataBeanList.addAll(bean.getData());
                            adapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });

    }

    //查询精准洗车列表
    private void initOkGoService1() {
        OkGo.<String>get(Config.s + "llc/fineCarServerController/queryFineCarServerList")
                .tag(this)
                .params("token", UserBean.getbToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FullServiceBean bean = gson.fromJson(response.body(), FullServiceBean.class);
                        if (bean.isIsSuccess()) {

                            dataBeanList.clear();

                            dataBeanList.addAll(bean.getData());
                            adapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });

    }

    //删除服务
    private void initDeleteService(final int pos) {
        OkGo.<String>get(Config.s + "llc/deleteCarService")
                .tag(this)
                .params("token", UserBean.getbToken(mActivity))
                .params("id",dataBeanList.get(pos).getId() )
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()) {
                            dataBeanList.remove(pos);
                            adapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });

    }

    //删除精准洗车服务
    private void initDeleteService1() {
        OkGo.<String>get(Config.s + "llc/fineCarServerController/deleteFineCarWashServer")
                .tag(this)
                .params("token", UserBean.getbToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()) {
                            dataBeanList.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });

    }

}
